﻿@{
    ViewBag.Title = "ExpiryDate";
}

@section scripts
{
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <link href="@Url.Content("~/content/css/bootstrap.css")" rel="stylesheet" type="text/css" />    
    <script src="@Url.Content("~/Scripts/common/export.js")" type="text/javascript"></script>
}

<form id="form1" method="post">
        <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
            <table style="width:100%;">
                <tr>
                    <td style="width:100%;">
                        <a class="mini-button" name="SaveInfo" iconCls="icon-ok" onclick="SaveInfoNew" plain="true">开始激活</a>                        
                        <span class="separator"></span>
                        <a class="mini-button" iconCls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                    </td>
                </tr>
            </table>                
        </div>
        <div name="Base" title="系统激活" style="height:98%;overflow:hidden;margin-top:5px;margin-left:5px">
            <table>
                <tr>
                    <td width="70px" height="32px" align="right">商户编码：</td>
                    <td width="150px" height="32px"  >
                        <input name="FNumber" id="FNumber" class="mini-textbox" style="width:150px;" required="true" enabled="false" value="" TabIndex="1"/>
                    </td>
                    <td width="70px"  height="32px"  align="right">商户名称：</td>
                    <td width="300px" height="32px"  >
                        <input id="FName" name="FName" class="mini-textbox " style="width:250px;"  enabled="false" TabIndex="2"/>
                    </td>
                </tr>
             </table>   
            <table>
                <tr>
                    <td width="70px"  height="32px"  align="right">用户数量：</td>
                    <td width="70px" height="32px"  >
                        <input name="FQty" class="mini-spinner" style="width: 70px;" minValue="1" maxValue="999999"
                        changeOnMousewheel ="false" showbutton="true" value="1"   onvaluechanged ="setAmoutTotal"/>
                    </td>
                    <td width="70px" height="32px"  align="right">续费年限：</td>
                    <td width="50px" height="32px"  >
                        <input name="FYears" class="mini-spinner" style="width: 60px;" minValue="0" maxValue="100"
                        changeOnMousewheel ="false" showbutton="true" value="1" onvaluechanged ="setAmoutTotal"/>
                    </td>
                    <td width="50px"  height="32px" align="right">+月份：</td>
                    <td width="60px" height="32px" >
                        <input name="FMonths" class="mini-spinner" style="width: 60px;" minValue="0" maxValue="100"
                        changeOnMousewheel ="false" showbutton="true" value="0" onvaluechanged ="setAmoutTotal"/>
                    </td>
                    <td width="70px"  height="32px"  align="right">合计金额：</td>
                    <td width="150px" height="32px"  >
                        <input name="FAllAmount" class="mini-spinner" enabled="false" style="width: 150px;" minValue="0" maxValue="999999"
                        changeOnMousewheel ="false" showbutton="false" readonly="true" value="0" TabIndex="3"/>
                    </td>
                    <td>
                        <div id="FLimitFlag" name="FLimitFlag" class="mini-checkbox" style="margin-left: 20px" readOnly="false" 
                        checked="false" text="无限期" onvaluechanged="limitCheck"></div>
                    </td>
                </tr>
             </table>  
            <div id="datagrid1" class="mini-datagrid" style="width:790px;height:350px;margin-top:5px;margin-left:10px" 
                pageSize="10" idField="FItemID" multiSelect="true"  allowCellSelect="true" allowCellEdit="true"showColumnsMenu="true"
                borderStyle="border:1;" url="GetCorpAppList" fitColumns="false" showSummaryRow="false"
                showPager = "false" showPageSize="false" showPageIndex="false" onlyCheckSelection ="true" selectOnLoad="true">
                <div property="columns">
                    <div type="checkcolumn"></div>      
                    <div type="indexcolumn" width="40" headerAlign="center" >序号</div>                    
                    <div field="FName" width="130" allowSort="true" headerAlign="center" >应用名</div>   
                    <div field="FRegDate" width="80" allowSort="true" dateFormat="yyyy-MM-dd" headerAlign="center" >注册日期</div> 
                    <div field="FEndDate" width="80" allowSort="true" dateFormat="yyyy-MM-dd" headerAlign="center" >到期日期</div>                     
                    <div field="FAgentPrice" name="FSalesPrice" width="80" numberFormat="#,0.00" headerAlign="center" align="right">单价/年</div>
                    <div field="FAgentAllPrice" name="FAgentAllPrice" width="80" numberFormat="#,0.00" headerAlign="center" align="right">单价/无限期</div>
                    <div field="FCount" width="70" allowSort="true" align="center" headerAlign="center" >用户数量
                        <input property="editor" class="mini-spinner" minValue="0" maxValue="99999999" style="width:100%;" 
                            changeOnMousewheel ="false" showbutton="false" increment ="1" />
                    </div> 
                    <div header="续费时长" headerAlign="center">
                        <div property="columns">
                            <div field="FYears" name="FYears" width="50" numberFormat="#" headerAlign="center" align="center">年</div>
                            <div field="FMonths" name="FMonths" width="50" numberFormat="#" headerAlign="center" align="center">月</div>
                        </div>
                    </div>                    
                    <div field="FAmount" name="FAmount" width="80" numberFormat="#,0.00" headerAlign="center" align="right">小计</div>
               </div>
            </div>  



            <table style="display:none;">
                <tr>
                    <td width="80px"  align="right">POS数量：</td>
                    <td width="190px" >
                        <input name="FPos" class="mini-spinner" style="width: 190px;" minValue="0" maxValue="999999"
                        changeOnMousewheel ="false" showbutton="false" value="1" TabIndex="3"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px"  align="right">管理后台：</td>
                    <td width="90px" >
                        <input name="FServer" class="mini-spinner" style="width: 90px;" minValue="0" maxValue="1" 
                        changeOnMousewheel ="false" value="1" showbutton="false" TabIndex="4"/>
                        <input id="FProductID" name ="FProductID" class="mini-combobox" style="width:95px;" value ="1"
                        showNullItem="false" allowInput="false" align="left" data ="dProductID"/>   
                    </td>
                </tr>
                <tr>
                    <td width="80px"  align="right">微商城：</td>
                    <td width="190px" >
                        <input name="FEshop" class="mini-spinner" style="width: 190px;" minValue="0" maxValue="1" 
                        changeOnMousewheel ="false" showbutton="false" value="0" TabIndex="5"/>
                    </td>
                </tr>
                <tr>
                    <td width="80px"  align="right">续费期限：</td>
                    <td width="190px" >
                        <input id="comDateType" name ="FDateType" class="mini-combobox" style="width:190px;" value ="1"
                        showNullItem="false" required="true" allowInput="false" align="left" data ="dDateType"/>   
                    </td>
                </tr>
            </table>
        </div>
    <input name="FItemID" class="mini-hidden" />
    <input name="FClassID" class="mini-hidden" />
    <input name="BillNo" value="@ViewBag.BillNo" class="mini-hidden" />
    <input name="BillID" value="@ViewBag.BillID" class="mini-hidden" />

</form>

<script>

    var dProductID = [{ "id": "1", "text": "普通版" },
                     { "id": "4", "text": "连锁版" }]

    var dDateType = [{ "id": "1", "text": "1年" },
                     { "id": "2", "text": "2年" },
                     { "id": "3", "text": "3年" },
                     { "id": "4", "text": "5年" },
                     { "id": "5", "text": "10年" },
                     { "id": "6", "text": "无限期" },
                     { "id": "0", "text": "一个月" }]
    var sumFields = "FAmount";    
    mini.parse();

    var grid = mini.get("datagrid1");
    function changeAll() {
        if (this.getChecked()) {
            mini.getbyName("FDays").setEnabled(false);
        }
        else {
            mini.getbyName("FDays").setEnabled(true);
        }
    }

    function setAmoutTotal() {
        var rows = grid.getSelecteds();
        var allamount = 0;
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            var mQty = mini.getbyName("FQty").getValue();
            var mConet = mQty;
            var mYears = mini.getbyName("FYears").getValue();
            var mMonths = mini.getbyName("FMonths").getValue();
            var mSalesPrice = row.FAgentPrice;

            if (row.FAppType != 1) {
                mConet = "不限";
                mQty = 1;
            }
            var mAmount = mQty * mSalesPrice * mYears + mQty * mSalesPrice * mMonths / 12;
            if (mini.getbyName("FYears").getValue() >= 100) {
                mSalesPrice = row.FAgentAllPrice;
                mAmount = mQty * mSalesPrice;
            }

            allamount = allamount + mAmount;
            grid.updateRow(row, { FQty: mQty, FCount: mConet, FYears: mYears, FMonths: mMonths, FAmount: mAmount });
        }
        grid.accept();
        mini.getbyName("FAllAmount").setValue(allamount.toFixed(2));
    }

    //加载资料
    function SetInfoData(data) {
        mini.getbyName("FItemID").setValue(data.FItemID);
        mini.getbyName("FClassID").setValue(data.FClassID);
        mini.getbyName("FNumber").setValue(data.FNumber);
        mini.getbyName("FName").setValue(data.FName);
        LoadListData(data.FItemID);
    }

    function LoadListData(mCorpID) {
        var json = "[{";
        json = json + "'mCorpID':" + "'" + mCorpID + "',";
        json = json + "'FSystemAppID':'" + parent.window.getSystemAppID() + "'}]";        
        $.ajax({
            url: 'GetCorpAppList',
            data: { mjson: json },
            type: "post",
            cache: false,
            success: function (txt) {                
                var dataR = mini.decode(txt);
                grid.setData(dataR.data);
                grid.selectAll(true);
                setAmoutTotal();
            },
            error: function (xhr) {
                mini.alert("异常:" + xhr.responseText);
            }
        });        
    }

    function GetClassID() {
        var FNumber = mini.getbyName("FNumber").getText();
        $.post("GetCorpClass", { json: "[{'FItemID':'" + FNumber + "', 'mSystemAppID':'" + parent.window.getSystemAppID() + "'}]" }, function (data) {
            var mdata = mini.decode(data);
            if (mdata.data.length > 0) {
                mini.getbyName("BillNo").setValue(mdata.BillOrder[0].FBillNo);
                mini.getbyName("BillID").setValue(mdata.BillOrder[0].FEntryID);
            }
        });
    }

    //保存资料
    function SaveInfo() {
        mini.getbyName("SaveInfo").disable();
        var form = new mini.Form("#form1");
        form.validate();
        if (form.isValid() == false) {
            mini.getbyName("SaveInfo").enable();
            return;
        }
        var data = form.getData(true, false);
        if (data.FDays <= 0) {
            mini.showTips({ content: '未设置要增加的天数', state: "danger", x: "center", y: "top", timeout: 3000 });
            mini.getbyName("SaveInfo").enable();
            return;
        }
        data.mSystemAppID = parent.window.getSystemAppID();        
        var json = mini.encode(data);
        var rows = grid.getSelecteds();
        var mlist = mini.encode(rows);
        $.post("SaveExpiry", { json: json, mdata: mlist }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                NewInfo();
            }
            else {
                mini.alert("异常:" + ret.error);
            }
            mini.getbyName("SaveInfo").enable();
        });
    }


    //保存资料
    function SaveInfoNew() {
        mini.getbyName("SaveInfo").disable();
        var form = new mini.Form("#form1");
        var data = form.getData(true, false);
        data.mSystemAppID = parent.window.getSystemAppID();
        var json = mini.encode(data);
        var rows = grid.getSelecteds();
        var mlist = mini.encode(rows);
        $.post("SaveExpiryNew", { json: json, mdata: mlist }, function (data) {
            var ret = mini.decode(data);
            if (ret.status == 1) {
                mini.showTips({ content: ret.msg, state: "success", x: "center", y: "top", timeout: 3000 });
            }
            else {
                mini.alert("异常:" + ret.error);
            }
            mini.getbyName("SaveInfo").enable();
        });
    }

    var years=1;
    function limitCheck() {
        if (mini.get("FLimitFlag").getChecked()) {
            if (mini.getbyName("FYears").getValue() < 100)
                years = mini.getbyName("FYears").getValue();
            mini.getbyName("FYears").setValue(100); 
            mini.getbyName("FYears").setEnabled(false);
            mini.getbyName("FMonths").setEnabled(false);
        }
        else {
            mini.getbyName("FYears").setValue(years);
            mini.getbyName("FYears").setEnabled(true);
            mini.getbyName("FMonths").setEnabled(true);
        }
        setAmoutTotal();
    }
</script>